<?php

class MySQLDatabase{

	private $conn;
	public $result;
	
	public function __construct(){
		$this->connect_database();
	}

	public function connect_database(){
		
		$this->conn = mysql_connect(DB_SERVER, DB_USER, DB_PASS);
		if(!$this->conn){
		
			die("Error in connecting to database" . mysql_error());
		
		} else {
			$select_db = mysql_select_db(DB_NAME);
			if(!$select_db){
				die("Error in selecting database" . mysql_error());
			}
		
		}
	
	}
	
	public function query($sql){
		$this->result = mysql_query($sql);
		if($this->result){
			return $this;
		} else {
			mysql_error();
			exit();
		}
	}
	
	public function num_rows($result_set){
	
		return mysql_num_rows($result_set, $this->conn);
	
	}
/*
	public function fetch_array($result_set){
	
		return mysql_fetch_array($result_set);
	
	}
*/
	public function fetch_array(){
		
		return mysql_fetch_array($this->result);
	}
	
	public function fetch_assoc(){
		
		$row = array();
		if(mysql_num_rows($this->result) > 0){
			while($result_set = mysql_fetch_assoc($this->result)){
				$row[] = $result_set;		
			}		
		}
		return $row;	
	}
	
	public function affected_rows() {
		return mysql_affected_rows($this->conn);
    }
	

	public function close_conn(){
	
		if(isset($this->conn)) {
			mysql_close($this->conn);
			unset($this->conn);
		}
	
	}

}

$db = new MySQLDatabase();
